package com.qhm123.spark.douban;

import java.io.IOException;
import java.util.List;

import com.google.gdata.client.douban.DoubanService;
import com.google.gdata.data.Link;
import com.google.gdata.data.Person;
import com.google.gdata.data.douban.Attribute;
import com.google.gdata.data.douban.CollectionEntry;
import com.google.gdata.data.douban.CollectionFeed;
import com.google.gdata.data.douban.Subject;
import com.google.gdata.data.douban.SubjectEntry;
import com.google.gdata.data.douban.Tag;
import com.google.gdata.data.douban.UserEntry;
import com.google.gdata.data.douban.UserFeed;
import com.google.gdata.util.ServiceException;
import com.qhm123.spark.model.Book;
import com.qhm123.spark.model.DbHelper;
import com.qhm123.spark.model.User;

public class InitDB {

	private static String apiKey = "046a0792404735c00d3e16b6c0d5a140";
	private static String secret = "4be055fdab5fe0d5";

	public static void main(String[] args) throws IOException, ServiceException {
		DoubanService myService = new DoubanService("ApplicationName", apiKey,
				secret);
		// 第一次，尚未获得AccessToken时，使用的代码
		// String url = myService.getAuthorizationUrl(null);
		//
		// System.out.println("AuthorizationUrl: " + url);
		// BufferedReader br = new BufferedReader(new
		// InputStreamReader(System.in));
		// String pin = br.readLine();
		//
		// ArrayList<String> accessToken = myService.getAccessToken();
		// for (String token : accessToken) {
		// System.out.println(token);
		// }
		myService.setAccessToken("a087f91444122ae6e197eb3ccc3680b2",
				"1814d12a5ffbe738");

		UserEntry me = myService.getAuthorizedUser();
		System.out.println(me.getUid());
		System.out.println(me.getId());
		UserFeed userFeed = myService.getUserContacts(1, 10);
		for (UserEntry user : userFeed.getEntries()) {
			User u = new User();
			String userId = user.getId().substring(
					user.getId().lastIndexOf('/') + 1);
			System.out.println(userId);
			u.id = userId;
			System.out.println(user.getTitle().getPlainText());
			u.name = user.getTitle().getPlainText();
			u.password = "password";

			DbHelper.getInstance().putUser(u);

			System.out.println("-----------------------------------");

			CollectionFeed collectionFeed = myService.getUserCollections(
					user.getUid(), "book", null, "read", 1, 5);
			for (CollectionEntry collection : collectionFeed.getEntries()) {
				Subject se = collection.getSubjectEntry();
				int rating = 3;
				if (collection.getRating() != null) {
					rating = collection.getRating().getValue();
				}
				System.out.println("rating: " + rating);
				Book b = new Book();
				String bookId = se.getId().substring(
						se.getId().lastIndexOf('/') + 1);
				System.out.println(bookId);
				b.id = bookId;
				SubjectEntry book = myService.getBook(bookId);

				System.out.println(se.getTitle().getPlainText());
				b.title = se.getTitle().getPlainText();

				try {
					System.out.println("Summary: "
							+ book.getSummary().getPlainText());
					b.summary = book.getSummary().getPlainText();
				} catch (Exception e) {
				}

				List<Link> links = book.getLinks();
				for (Link link : links) {
					if (link.getRel().equals("image")) {
						System.out.println(link.getHref() + ": "
								+ link.getRel());
						b.imageurl = link.getHref();
					}
				}

				List<Person> authors = se.getAuthors();
				for (Person author : authors) {
					System.out.println("author: " + author.getName());
					b.author = author.getName();
				}

				List<Tag> tags = book.getTags();
				System.out.println("tags len: " + tags.size());
				for (Tag tag : tags) {
					System.out.println(tag.getName() + ": " + tag.getCount());
					String tagId = String.valueOf(DbHelper.getInstance()
							.putTag(tag.getName()));
					DbHelper.getInstance().putBookTag(bookId, tagId);
				}

				List<Attribute> attrs = se.getAttributes();
				for (Attribute attr : attrs) {
					System.out.println(attr.getName() + ": "
							+ attr.getContent());
					if (attr.getName().equals("isbn10")) {
						b.isbn = attr.getContent();
					} else if (attr.getName().equals("translator")) {
						b.translator = attr.getContent();
					} else if (attr.getName().equals("price")) {
						b.price = attr.getContent();
					} else if (attr.getName().equals("publisher")) {
						b.publisher = attr.getContent();
					} else if (attr.getName().equals("pubdate")) {
						b.pubdate = attr.getContent();
					}
				}
				DbHelper.getInstance().putBook(b);
				DbHelper.getInstance().putRating(userId, bookId, rating);
			}
		}

	}
}
